﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.Util;

public partial class DataStorage_IndicatorsManage : System.Web.UI.Page
{
    Param MyP = new Param();
    protected int colNum = 4;
    Dictionary<string, string> dic = new Dictionary<string, string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Indicator.Text = Request.QueryString["sIndicator"].ToString().Trim();
            sltStartYearsDataBind();
            sltEndYearsDataBind();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sStart = sltStartYears.SelectedValue.Trim();
        string sEnd = sltEndYears.SelectedValue.Trim();
        if (String.IsNullOrEmpty(sStart))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择开始年份！');</script>");
            return;
        }
        if (String.IsNullOrEmpty(sEnd))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择结束年份！');</script>");
            return;
        }
        if (int.Parse(sStart) > int.Parse(sEnd))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('开始年份应该小于结束年份，请修改！');</script>");
            return;
        }
        int sLast = int.Parse(sStart) - 1;
        GetExportList(sLast.ToString(), sEnd);
        hidState.Value = "1";
    }

    private void sltStartYearsDataBind()
    {
        sltStartYears.DataTextField = "Name";
        sltStartYears.DataValueField = "Name";
        sltStartYears.DataSource = MyP.GetCodeByParentCode("2");
        sltStartYears.DataBind();
        sltStartYears.Items.Insert(0, "请选择...");
        sltStartYears.Items[0].Value = "";
    }
    private void sltEndYearsDataBind()
    {
        sltEndYears.DataTextField = "Name";
        sltEndYears.DataValueField = "Name";
        sltEndYears.DataSource = MyP.GetCodeByParentCode("2");
        sltEndYears.DataBind();
        sltEndYears.Items.Insert(0, "请选择...");
        sltEndYears.Items[0].Value = "";
    }
    private void GetExportList(string sStart, string sEnd)
    {
        String sTD = String.Empty;
        String sTitle = String.Empty;
        String sTitle2 = String.Empty;
        String sFormula = String.Empty;
        //1.获取需要的数据
        DataView mydv = SQLHelper.ExecuteDataView("select * from vi_Planning where years>='" + sStart + "' and years<='" + sEnd + "'");
        colNum += mydv.Count;
        //2.填充数值
        DataTable dt = mydv.Table;
        for (int i = 0; i < mydv.Count; i++)
        {
            sTitle2 += "<td style=\"text-align: center\"><strong>实际值</strong></td>";
            if (mydv[i][0].ToString() != DateTime.Now.Year.ToString())
            {
                sTitle += "<td style=\"text-align: center\"><strong>" + mydv[i][0] + "年</strong></td>";
            }
            else
            {
                String maxMonth = SQLHelper.ExecuteString("select MAX(Years) from tb_ReportAnalysis");
                sTitle += "<td style=\"text-align: center\"><strong>" + DateTime.Now.Year + "年截至" + maxMonth.Replace(DateTime.Now.Year.ToString(),"") + "月</strong></td>";
            }
        }
        lblTableHead.Text = sTitle;
        lblTableHead2.Text = sTitle2;
        for (int i = 1; i < dt.Columns.Count; i++)
        {
            sTD = String.Empty;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                sTD += "<td>" + mydv[j][i] + "</td>";
            }
            Label lbl = (Label)this.FindControl("Label" + i);
            lbl.Text = sTD;
        }
        DataView lastYearValue = SQLHelper.ExecuteDataView("select Indicators_1,Indicators_2,Indicators_3,Indicators_4,Indicators_5,Indicators_6," +
                                                            "Indicators_11,Indicators_15,Indicators_16 from tb_EnergySave where years='" + sStart + "'");
        dic.Clear();
        for (int i = 61; i < 75; i++)
        {
            sTD = String.Empty;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (j != 0)
                {
                    string tmp = GetFormulaValue(mydv, i - 60, j, j + 5);
                    sTD += "<td>" + tmp + "</td>";
                    //sTD += "<td>" + GetFormula(i - 60, j + 5) + "</td>";
                    dic.Add(GetDYG(j + 5) + (i + 4), tmp);
                }
                else
                {
                    if (i - 60 < lastYearValue.Count)
                    {
                        sTD += "<td>" + lastYearValue[0][i - 60].ToString() + "</td>";
                        dic.Add(GetDYG(j + 5) + (i + 4), lastYearValue[0][i - 60].ToString());
                    }
                    else
                    {
                        sTD += "<td>&nbsp;</td>";
                        dic.Add(GetDYG(j + 5) + (i + 4), "");
                    }

                }
            }
            Label lblF = (Label)this.FindControl("Label" + i);
            lblF.Text = sTD;
        }
    }

    private string GetDYG(int iNum)
    {
        return "F".Replace("F", GetRow(iNum));
    }
    /// <summary>
    /// 获取公式
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    private string GetFormula(int iRow, int iNum)
    {
        string sRtn = String.Empty;
        switch (iRow)
        {
            case 1:
                sRtn = "=F5/(F19+F20)*10000".Replace("F", GetRow(iNum));
                break;
            case 2:
                sRtn = "=(F65-E65)/E65".Replace("F", GetRow(iNum)).Replace("E", GetRow(iNum - 1));
                break;
            case 3:
                sRtn = "=F5/F23".Replace("F", GetRow(iNum));
                break;
            case 4:
                sRtn = "=(F67-E67)/E67".Replace("F", GetRow(iNum)).Replace("E", GetRow(iNum - 1));
                break;
            case 5:
                sRtn = "=F5/F21".Replace("F", GetRow(iNum));
                break;
            case 6:
                sRtn = "=(F69-E69)/E69".Replace("F", GetRow(iNum)).Replace("E", GetRow(iNum - 1));
                break;
            case 7:
                sRtn = "=F24/F19".Replace("F", GetRow(iNum));
                break;
            case 8:
                sRtn = "=F61/F60".Replace("F", GetRow(iNum));
                break;
            case 9:
                sRtn = "=F64/F63".Replace("F", GetRow(iNum));
                break;
            case 10:
                sRtn = "=F14/F22*10000".Replace("F", GetRow(iNum));
                break;
            case 11:
                sRtn = "=F7/F19".Replace("F", GetRow(iNum));
                break;
            case 12:
                sRtn = "=F10/F26".Replace("F", GetRow(iNum));
                break;
            case 13:
                sRtn = "=F8/F27".Replace("F", GetRow(iNum));
                break;
            case 14:
                sRtn = "=F11/F28".Replace("F", GetRow(iNum));
                break;
        }
        return sRtn;
    }

    /// <summary>
    /// 获取公式对应的值
    /// </summary>
    /// <param name="iRow"></param>
    /// <param name="iNum"></param>
    /// <returns></returns>
    private string GetFormulaValue(DataView mydv, int iRow, int iNum, int iNumH)
    {
        string sRtn = String.Empty;
        string sTmp1 = String.Empty;
        string sTmp2 = String.Empty;
        string sTmp3 = String.Empty;
        decimal dec = 0;
        switch (iRow)
        {
            case 1:
                //=F5/(F19+F20)*10000
                sTmp1 = mydv[iNum][1].ToString() == String.Empty ? "0" : mydv[iNum][1].ToString();
                sTmp2 = mydv[iNum][15].ToString() == String.Empty ? "0" : mydv[iNum][15].ToString();
                sTmp3 = mydv[iNum][16].ToString() == String.Empty ? "0" : mydv[iNum][16].ToString();
                if (sTmp2 == "0" && sTmp3 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / (decimal.Parse(sTmp2) + decimal.Parse(sTmp3)) * 10000;
                break;
            case 2:
                //=(F65-E65)/E65
                sTmp1 = dic["F65".Replace("F", GetRow(iNumH))];
                sTmp1 = sTmp1 == String.Empty ? "0" : sTmp1;
                sTmp2 = dic["E65".Replace("E", GetRow(iNumH - 1))];
                sTmp2 = sTmp2 == String.Empty ? "0" : sTmp2;
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = (decimal.Parse(sTmp1) - decimal.Parse(sTmp2)) / decimal.Parse(sTmp2);
                break;
            case 3:
                //=F5/F23
                sTmp1 = mydv[iNum][1].ToString() == String.Empty ? "0" : mydv[iNum][1].ToString();
                sTmp2 = mydv[iNum][19].ToString() == String.Empty ? "0" : mydv[iNum][19].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 4:
                //=(F67-E67)/E67
                sTmp1 = dic["F67".Replace("F", GetRow(iNumH))];
                sTmp1 = sTmp1 == String.Empty ? "0" : sTmp1;
                sTmp2 = dic["E67".Replace("E", GetRow(iNumH - 1))];
                sTmp2 = sTmp2 == String.Empty ? "0" : sTmp2;
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = (decimal.Parse(sTmp1) - decimal.Parse(sTmp2)) / decimal.Parse(sTmp2);
                break;
            case 5:
                //=F5/F21
                sTmp1 = mydv[iNum][1].ToString() == String.Empty ? "0" : mydv[iNum][1].ToString();
                sTmp2 = mydv[iNum][17].ToString() == String.Empty ? "0" : mydv[iNum][17].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 6:
                //=(F69-E69)/E69
                sTmp1 = dic["F69".Replace("F", GetRow(iNumH))];
                sTmp1 = sTmp1 == String.Empty ? "0" : sTmp1;
                sTmp2 = dic["E69".Replace("E", GetRow(iNumH - 1))];
                sTmp2 = sTmp2 == String.Empty ? "0" : sTmp2;
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = (decimal.Parse(sTmp1) - decimal.Parse(sTmp2)) / decimal.Parse(sTmp2);
                break;
            case 7:
                //=F24/F19  
                sTmp1 = mydv[iNum][20].ToString() == String.Empty ? "0" : mydv[iNum][20].ToString();
                sTmp2 = mydv[iNum][15].ToString() == String.Empty ? "0" : mydv[iNum][15].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 8:
                //=F61/F60
                sTmp1 = mydv[iNum][57].ToString() == String.Empty ? "0" : mydv[iNum][57].ToString();
                sTmp2 = mydv[iNum][56].ToString() == String.Empty ? "0" : mydv[iNum][56].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 9:
                //=F64/F63
                sTmp1 = mydv[iNum][60].ToString() == String.Empty ? "0" : mydv[iNum][60].ToString();
                sTmp2 = mydv[iNum][59].ToString() == String.Empty ? "0" : mydv[iNum][59].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 10:
                //F14/F22*10000
                sTmp1 = mydv[iNum][10].ToString() == String.Empty ? "0" : mydv[iNum][10].ToString();
                sTmp2 = mydv[iNum][18].ToString() == String.Empty ? "0" : mydv[iNum][18].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2) * 10000;
                break;
            case 11:
                //=F7/F19
                sTmp1 = mydv[iNum][3].ToString() == String.Empty ? "0" : mydv[iNum][3].ToString();
                sTmp2 = mydv[iNum][15].ToString() == String.Empty ? "0" : mydv[iNum][15].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 12:
                //=F10/F26
                sTmp1 = mydv[iNum][6].ToString() == String.Empty ? "0" : mydv[iNum][6].ToString();
                sTmp2 = mydv[iNum][22].ToString() == String.Empty ? "0" : mydv[iNum][22].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 13:
                //=F8/F27
                sTmp1 = mydv[iNum][4].ToString() == String.Empty ? "0" : mydv[iNum][4].ToString();
                sTmp2 = mydv[iNum][23].ToString() == String.Empty ? "0" : mydv[iNum][23].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
            case 14:
                //=F11/F28
                sTmp1 = mydv[iNum][7].ToString() == String.Empty ? "0" : mydv[iNum][7].ToString();
                sTmp2 = mydv[iNum][24].ToString() == String.Empty ? "0" : mydv[iNum][24].ToString();
                if (sTmp2 == "0")
                    dec = 0;
                else
                    dec = decimal.Parse(sTmp1) / decimal.Parse(sTmp2);
                break;
        }
        sRtn = dec.ToString("0.00");
        if (sRtn == "0.00")
            return "";
        else
            return sRtn;
    }
    private string GetRow(int iNum)
    {
        StringBuilder sbW = new StringBuilder();
        if (iNum % 26 == 0)
        {
            sbW.Insert(0, 'Z');
            iNum = iNum / 26 - 1;
        }
        else
        {
            sbW.Insert(0, (char)((iNum % 26 - 1) + 'A'));
            iNum /= 26;
        }
        return sbW.ToString();
    }
}
